from md_toc import build_toc
import re

toc = build_toc("readme.md", keep_header_levels=5)

with open("readme.md", "rb") as f:
    text = f.read().decode("utf-8")
text = re.sub("<!-- toc -->.*?<!-- toc -->", "<!-- toc -->\n%s<!-- toc -->" % toc, text, 0, re.M | re.S)
if text:
    with open("readme.md", "wb") as f:
        f.write(text.encode("utf-8"))

